﻿#include "qmllogsbrowser.h"

QmlLogsBrowser::QmlLogsBrowser(QObject *parent)
    : QObject{parent}
{}

QmlLogsBrowser *QmlLogsBrowser::getInstance()
{
    static QmlLogsBrowser *myQmlLogsBrowser = new QmlLogsBrowser();
    return myQmlLogsBrowser;
}

void QmlLogsBrowser::logsPrintSlot(QString s)
{
    QString datetime_str = QDateTime::currentDateTime().toString(QString("yyyy/MM/dd HH:mm:ss:zzz "));
    QString log = "[" + datetime_str + "] " + s;
    emit pushLogsToQml(log);
}

void QmlLogsBrowser::log(QString s)
{
    QString datetime_str = QDateTime::currentDateTime().toString(QString("yyyy/MM/dd HH:mm:ss:zzz "));
    QString log = "[" + datetime_str + "] " + s;
    emit pushLogsToQml(log);
}

void QmlLogsBrowser::log(const char *format, ...)
{
    static char buffer[8192] = {0};
    va_list va_param;
    va_start(va_param, format);
    vsnprintf(buffer, sizeof(buffer) - 1, format, va_param);
    va_end(va_param);

    QString datetime_str = QDateTime::currentDateTime().toString(QString("yyyy/MM/dd HH:mm:ss:zzz "));
    QString log = "[" + datetime_str + "] " + buffer + '\n';

    emit pushLogsToQml(log);
}
